--[[ 武装飞鸟2 FBN用脚本,不同角色数据不同,以Tavia为准
  使用了新的自制FBN库,有自制的宏函数,传入宏表运行
  
  By setycyas @2025-02-15
]]

myFBN = require('MyFBN')
myFBN.X_RESOLUTION, myFBN.Y_RESOLUTION = 320, 224

-- 本模块
local GAME = {
  -- 坐标,memory.readword
  Y_ADDR = 0x06055011-1,
  X_ADDR = 0x06055014,
  -- 火力与蓄力地址,readbyte,readword
  POW_ADDR = 0x0605505b,
  CHARGE_ADDR = 0x06055074,
  -- 中心显示方块的宽和高,单位为像素
  BOX_HALF_WIDTH = 2,
  BOX_HALF_HEIGHT = 2,
  -- 中心方块的边框颜色和中心颜色
  BOX_BORDER_COLOR = 0xff0000DD,
  BOX_COLOR = 0xff000088,
  -- 像素平移
  X_OFFSET = 0,
  Y_OFFSET = -1,
  -- 蓄力情况不同的颜色
  CHARGE_COLOR0 = 0xaaaaaaAA,
  CHARGE_COLOR1 = 0x00ff00AA,
  CHARGE_COLOR2 = 0xDfd700AA,
  -- 蓄力等级需要的数值,貌似不同人物不同,这里用了TAVIA的数据
  CHARGE_LV1 = 0x300,
  CHARGE_LV2 = 0xC00,
}

-- 获取坐标
function GAME.getXY()
  local x, y
  x, y = memory.readword(GAME.X_ADDR), memory.readword(GAME.Y_ADDR)
  --print(x, y)
  return x+GAME.X_OFFSET, y+GAME.Y_OFFSET
end

-- 画辅助线
function GAME.showAssist()
  --gui.box(110, 0, 114, 12, "BLUE", "BLACK")
  gui.box(0, 111, 10, 113, "BLUE", "BLACK")
end

-- 显示火力等级
function GAME.showPow()
  local power, str1, color
  power = memory.readbyte(GAME.POW_ADDR)
  color = 0x00dd00AA
  if (power == 0) then
    str1 = "X"
    color = "white"
  elseif (power == 1) then
    str1 = "P"
  elseif (power == 2) then
    str1 = "P+P"
  else
    str1 = "FULL!!!"
    color = 0x00dd00AA
  end    
  gui.text(10, 0, str1, color)
end

-- 画中心十字线
function GAME.showCenter()
  local x, y, charge, lineColor
  -- 获取数据
  x, y = GAME.getXY()
  charge = memory.readword(GAME.CHARGE_ADDR)
  --print(string.format("%x", charge))
  if (charge > GAME.CHARGE_LV2) then
    lineColor = GAME.CHARGE_COLOR2
  elseif (charge > GAME.CHARGE_LV1) then
    lineColor = GAME.CHARGE_COLOR1
  else
    lineColor = GAME.CHARGE_COLOR0
  end
  -- 画线
  myFBN.xLine(x, 1, lineColor)
  myFBN.yLine(y, 1, lineColor)
  -- 画中心
  myFBN.centerBox(x, y, GAME.BOX_HALF_WIDTH, GAME.BOX_HALF_HEIGHT)
end

-- 每帧运行函数
function GAME.Main()
  GAME.showPow()
  GAME.showAssist()
  GAME.showCenter()
end

-- 宏表
GAME.macros = {}
-- 向左3帧
GAME.macros['left3'] = {
  {["P1 Left"] = true}, {["P1 Left"] = true}, {["P1 Left"] = true}
}
-- 向右3帧
GAME.macros['right3'] = {
  {["P1 Right"] = true}, {["P1 Right"] = true}, {["P1 Right"] = true}
}
-- 设定宏的函数
function GAME.setMacros()
  myFBN.setHotkey(1, GAME.macros['left3'])
  myFBN.setHotkey(2, GAME.macros['right3'])
end


--[[运行]]
-- 设定宏
GAME.setMacros()
-- 设定每帧运行
myFBN.runPerFrame(GAME.Main)

return GAME
